package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Discoverylink;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("DiscoverylinkController")
public class DiscoverylinkController extends CmiEmaController {

	@RequestMapping("/listDiscoverylinks")
	public ModelAndView listDiscoverylinks(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = discoverylinkPaginationService.updatePagination(request, pagination);
		Set<Discoverylink> discoverylinks = discoverylinkPaginationService.getPaginationDiscoverylinks(pagination);

		mav.addObject("discoverylinks", discoverylinks);
		mav.addObject("entityName", "discoverylink");
		mav.addObject("pagination", pagination);
		mav.setViewName("discoverylink/listDiscoverylinks");
		return mav;
	}

	public String listDiscoverylinks() {
		return "redirect:/listDiscoverylinks";
	}

	@RequestMapping("/viewDiscoverylink")
	public ModelAndView viewDiscoverylink(@RequestParam Integer discoverylinkId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("discoverylink", discoverylinkDAO.findDiscoverylinkByPrimaryKey(discoverylinkId));
		mav.setViewName("discoverylink/viewDiscoverylink");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteDiscoverylinks")
	public ModelAndView confirmDeleteDiscoverylinks(
			@RequestParam(value = "deleteDiscoverylinksIds", required = true) Set<Integer> deleteDiscoverylinksIds) {
		ModelAndView mav = new ModelAndView();

		Set<Discoverylink> deleteDiscoverylinks = discoverylinkDAO.findDiscoverylinksByIds(deleteDiscoverylinksIds);
		Set<Discoverylink> referencedDiscoverylinks = discoverylinkService
				.loadReferenceDiscoverylinks(deleteDiscoverylinks);
		boolean hasReferenced;
		if (referencedDiscoverylinks.size() == 0) {
			mav.addObject("deleteEntitys", deleteDiscoverylinks);
			hasReferenced = false;
		} else {
			deleteDiscoverylinks.removeAll(referencedDiscoverylinks);
			mav.addObject("referencedEntitys", referencedDiscoverylinks);
			mav.addObject("deleteEntitys", deleteDiscoverylinks);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Discoverylink");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listDiscoverylinks");
		return mav;
	}

	@RequestMapping("/deleteDiscoverylink")
	public String deleteDiscoverylink(@RequestParam Integer discoverylinkId) {
		Discoverylink discoverylink = discoverylinkDAO.findDiscoverylinkByPrimaryKey(discoverylinkId);
		discoverylinkService.deleteDiscoverylink(discoverylink);
		return "forward:/listDiscoverylinks";
	}

	@RequestMapping("/confirmDeleteDiscoverylink")
	public ModelAndView confirmDeleteDiscoverylink(@RequestParam Integer discoverylinkId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("discoverylink", discoverylinkDAO.findDiscoverylinkByPrimaryKey(discoverylinkId));

		mav.setViewName("discoverylink/deleteDiscoverylink");

		return mav;
	}

	@RequestMapping("/deleteDiscoverylinks")
	public String deleteDiscoverylinks(
			@RequestParam(value = "deleteDiscoverylinksId", required = true) Set<Integer> deleteDiscoverylinksId) {
		discoverylinkService.deleteDiscoverylinks(deleteDiscoverylinksId);
		return "forward:/listDiscoverylinks";
	}

	@RequestMapping("/editDiscoverylink")
	public ModelAndView editDiscoverylink(@RequestParam Integer discoverylinkId) {

		ModelAndView mav = new ModelAndView();

		Discoverylink discoverylink = discoverylinkDAO.findDiscoverylinkByPrimaryKey(discoverylinkId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("discoverylink", discoverylink);

		// 把所有的引用discoverylink对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(discoverylinkService.loadDiscoverylinkObjectReferencedSet(discoverylink));

		// 将编辑页面discoverylink添加到mav中
		mav.setViewName("discoverylink/editDiscoverylink");
		return mav;
	}

	@RequestMapping("/newDiscoverylink")
	public ModelAndView newDiscoverylink() {
		ModelAndView mav = new ModelAndView();
		Discoverylink discoverylink = new Discoverylink();

		mav.addObject("discoverylink", discoverylink);

		// 把所有的引用discoverylink对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(discoverylinkService.loadDiscoverylinkObjectReferencedSet(discoverylink));

		mav.addObject("newFlag", true);
		mav.setViewName("discoverylink/editDiscoverylink");

		return mav;
	}

	@RequestMapping("/saveDiscoverylink")
	public ModelAndView saveDiscoverylink(@ModelAttribute Discoverylink discoverylink) {

		ModelAndView mav = new ModelAndView();

		Discoverylink existingDiscoverylink = CmiHibernateProxyUtil
				.deproxy(discoverylinkDAO.findDiscoverylinkByPhysicalPrimaryKey(discoverylink.getName()));

		// 新建记录
		if (discoverylink.getId() == null) {
			if (discoverylink.equals(existingDiscoverylink)) {

				mav.addAllObjects(discoverylinkService.loadDiscoverylinkClassReferencedSet(discoverylink));
				// mav = editContinue(mav, discoverylink);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("discoverylink/editDiscoverylink");
			} else {
				discoverylinkService.saveDiscoverylink(discoverylink);
				mav.setViewName("forward:/listDiscoverylinks");
			}
		}
		// 编辑记录
		else {
			Discoverylink originDiscoverylink = CmiHibernateProxyUtil
					.deproxy(discoverylinkDAO.findDiscoverylinkByPrimaryKey(discoverylink.getId()));
			if (!discoverylink.equals(originDiscoverylink) && discoverylink.equals(existingDiscoverylink)) {
				mav.addAllObjects(discoverylinkService.loadDiscoverylinkClassReferencedSet(discoverylink));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("discoverylink/editDiscoverylink");
			} else {
				discoverylinkService.saveDiscoverylink(discoverylink);
				mav.setViewName("forward:/listDiscoverylinks");
			}
		}
		return mav;
	}

	@RequestMapping("/discoverylinkController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
